<?php
namespace Viettrans\FrontBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Ressource controller.
 *
 * @Route("/ressource")
 */
class RessourceController extends Controller {
    /**
     * Generate Css
     *
     * @Route("/css/{route}.css", name="ressource_css")
     */
	public function styleAction($route) {

		$cssBase = array(
			'960_24_col',
			'main',
			'buttons',
			'header',
			'menu',
			'breadscrumb',
			'footer',
		);
		
		switch($route) {
			# Announcement ----------------------------------------- 
			case 'announcement_offer':
			case 'announcement_demand':
				$cssOwn = array(
					'townAutocomplete',
					'search',
					'pagination',
					'announcement_index',
					'jquery.fancybox-1.3.4',
				);
				break;
			
			case 'announcement_new':
				$cssOwn = array(
					'townAutocomplete',
					'announcement_new',
				);
				break;

			case 'announcement_preview':
				$cssOwn = array(
					'announcement_preview',
				);
				break;

			case 'announcement_show':
				$cssOwn = array(
					'announcement_show',
				);
				break;
			
			# User -------------------------------------------- 
			case 'user_new':
				$cssOwn = array(
					'townAutocomplete',
					'user_new',
				);
				break;

			case 'user_login':
				$cssOwn = array(
					'user_login',
				);
				break;				

			case 'user_forgot_password':
				$cssOwn = array(
					'user_forgot_password',
				);
				break;	
				
			# Default --------------------------------------------
			case 'default_success':
			case 'default_error':
			case 'default_intro':
			case 'default_sitemap':
			case 'default_help':
			case 'default_contact':
				$cssOwn = array();
				break; 
			
			# Default --------------------------------------------
			default:
				$cssOwn = array();
				$cssBase = array();
				break;
		}
		
		# Merge two lists
		$cssFiles = array_merge($cssBase, $cssOwn);
		
		if (empty($cssFiles)) return new Response('Not found', '404');
		
		$cssContentPacked = '';
		
		# Get Packer service
		$packer = $this->get('my_packer');
		
		
		foreach($cssFiles as $cssFile) {
			$cssContentPacked .= "\n/* $cssFile */\n";
			
			$cssContent = file_get_contents($this->container->getParameter('css_path').'/'.$cssFile.'.css');

			# Replace the constants
			$constants = array();
			$values = array();
			$cssContent = str_replace($constants, $values, $cssContent);
			
			$cssContentPacked .= $cssContent."\n";
		}
		
		# Return
		$response = new Response($cssContentPacked);
		$response->headers->set('Content-Type', 'text/css');
		
		return $response;
	}
	
    /**
     * Generate Js
     *
     * @Route("/js/{route}.js", name="ressource_js")
     */
	public function scriptAction($route) {

		$jsBase = array(
			'jquery-1.6.2.min',
			'main',
		);
		
		switch($route) {
			# Announcement ----------------------------------------- 
			case 'announcement_offer':
			case 'announcement_demand':
				$jsOwn = array(
					'jquery.myAutocomplete',
					'townAutocomplete',
					'jquery.myDefaultValue',
					'announcement_index',
					'search',
					'jquery.fancybox-1.3.4.pack',
				);
				break;
			
			case 'announcement_new':
				$jsOwn = array(
					'jquery.myAutocomplete',
					'townAutocomplete',
					'announcement_new',
				);
				break;

			case 'announcement_show':
				$jsOwn = array(
					'checkForm',
					'announcement_show',
				);
				break;
			
			# User -------------------------------------------- 
			case 'user_login':
				$jsOwn = array();
				break;
				
			case 'user_edit':
				$jsOwn = array(
					'jquery.myAutocomplete',
					'townAutocomplete',
					'user_edit',
				);
				break;
			
			case 'user_new':
				$jsOwn = array(
					'townAutocomplete',
					'user_new',
				);
				break;
				
			# Default --------------------------------------------
			case 'default_success':
			case 'default_error':
			case 'default_intro':
			case 'default_sitemap':
			case 'default_help':
			case 'default_contact':
				$jsOwn = array();
				break;
			
			# ----------------------------------------------------
			default:
				$jsOwn = array();
				$jsBase = array();
				break;
		}
		
		# Merge two lists
		$jsFiles = array_merge($jsBase, $jsOwn);
		
		if (empty($jsFiles)) return new Response('Not found', '404');
		
		$jsContentPacked = '';
		
		# Get Packer service
		$packer = $this->get('my_packer');
		
		
		foreach($jsFiles as $jsFile) {
			$jsContentPacked .= "\n/* $jsFile */\n";
			
			$jsContent = file_get_contents($this->container->getParameter('js_path').'/'.$jsFile.'.js');

			# Replace the constants
			$constants = array();
			$values = array();
			$jsContent = str_replace($constants, $values, $jsContent);
			
			$jsContentPacked .= $jsContent."\n";
		}
		
		# Return
		$response = new Response($jsContentPacked);
		$response->headers->set('Content-Type', 'text/javascript');
		
		return $response;
	}
}